package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

func Buypackages(ctx *gin.Context) {

	var users []models.Buypackage
	data_source.Db.Find(&users)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return

}

type BuypackageaddBody struct {
	ID        int64   `gorm:"column:id" json:"id"`
	Price     float64 `gorm:"column:price" json:"price"`
	Songprice float64 `gorm:"column:songprice" json:"songprice"`
	Tuijian   int64   `gorm:"column:tuijian" json:"tuijian"`
	Title     string  `gorm:"column:title" json:"title"`
}

func Buypackageadd(ctx *gin.Context) {
	request := BuypackageaddBody{}
	ctx.ShouldBind(&request)
	price := request.Price
	songprice := request.Songprice
	tuijian := request.Tuijian
	title := request.Title
	buy := models.Buypackage{
		Price:     price,
		Songprice: songprice,
		Tuijian:   tuijian,
		Title:     title,
	}
	data_source.Db.Create(&buy)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Buypackageupdate(ctx *gin.Context) {
	request := BuypackageaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	price := request.Price
	songprice := request.Songprice
	tuijian := request.Tuijian
	title := request.Title
	buy := models.Buypackage{
		Price:     price,
		Songprice: songprice,
		Tuijian:   tuijian,
		Title:     title,
	}
	data_source.Db.Where("id = ?", id).Updates(&buy)
	if songprice == 0 {
		data_source.Db.Model(&models.Buypackage{}).Where("id = ?", id).Update("songprice", songprice)

	}
	if title == "" {
		data_source.Db.Model(&models.Buypackage{}).Where("id = ?", id).Update("title", title)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func Buypackagedel(ctx *gin.Context) {
	request := ProcuresBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Buypackage{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
